local ____exports = {}
local ____DoraX = require("DoraX")
local React = ____DoraX.React
local toNode = ____DoraX.toNode
local useRef = ____DoraX.useRef
local ____Dora = require("Dora")
local Ease = ____Dora.Ease
local Line = ____Dora.Line
local Scale = ____Dora.Scale
local Vec2 = ____Dora.Vec2
local Director = ____Dora.Director
local Audio = ____Dora.Audio 
local Color = ____Dora.Color

local function GameControls(props)

    return React.createElement(
        "draw-node",
        {
            x = 250,
            y = 200
        },
        React.createElement(
            "draw-node",
            {
                touchEnabled = true,
                swallowTouches = true,
                onTapped = function()
                    local Director = require("Dora").Director
                    Director.entry:removeAllChildren()
                    gameState.level = 1
                    gameState.score = 0
                    gameState.canShoot = true
                    gameState.gameOver = false
                    gameState.victory = false
                    currentBirdType = 1
                    if score.current then
                        score.current.text = "得分: " .. gameState.score
                    end
                    
                    if props.bird and props.bird.current then
                        props.bird.current.position = Vec2(-850, -50)
                        props.bird.current.velocity = Vec2.zero
                        props.bird.current.tag = ""
                    end
                    initializeGame()
                end
            },
            
            React.createElement("rect-shape", {
                width = 80,
                height = 30,
                fillColor =0xff9370D8,
                borderWidth = 2,
                borderColor = 0xff000000
            }),
            React.createElement("label", {
                fontName = "simsun",
                fontSize = 12
            }, "重新开始")
        ) 
    )
end

local function initializeGame()
    Audio:playStream("Audio/birdbg.wav", true)
    local gameState = {
        birdsLeft = 3,
        score = 0,
        targetScore = 600, 
        canShoot = true,
        gameOver = false,
        victory = false,
        powerMultiplier = 1.0
    }

    local birdTypes = {
        {name = "小鸟",  power = 1.0, special = "直线冲击", color = 0xffff0000},
    }

    local currentBirdType = 1

    local function resetBirdToInitialPosition(bird)
        if bird and bird.current then
            bird.current.velocity = Vec2.zero
            bird.current.angularVelocity = 0
            bird.current.angle = 0
            
            bird.current.position = Vec2(-800, -50)
            
            bird.current.tag = ""
           
            gameState.canShoot = true
            
        end
    end

    local function OuYueBuilding(props)
        local imageFiles = {
            "Image/1.jpg",
            "Image/2.jpg", 
            "Image/3.jpg",
            "Image/4.jpg",
            "Image/5.jpg",
            "Image/6.jpg",
            "Image/7.jpg",  
            "Image/8.jpg",  
            "Image/9.jpg"   
        }
        
        local imageIndex = ((props.num - 10) % 9) + 1
        local imageFile = imageFiles[imageIndex]
        
        return React.createElement(
            "body",
            {
                type = "Dynamic",  
                scaleX = 0,
                scaleY = 0,
                x = props.x,
                y = props.y,
                tag = tostring(props.num)
            },
            React.createElement("rect-fixture", {width = 80, height = 80}),
            React.createElement(
                "sprite",
                {
                    file = imageFile, 
                    scaleX = 0.38,
                    scaleY = 0.38
                }
            ),
            React.createElement("label", {
                fontName = "simsun", 
                fontSize = 8,
                x = 0,
                y = 50
            }, foodName),
            React.createElement("label", {
                fontName = "simsun", 
                fontSize = 12,
                x = 0,
                y = -50
            }, tostring(props.num)),
            props.children
        )
    end

    local function OuYueSlingshot()
        return React.createElement(
            "draw-node",
            {
                x = -800,
                y = -320
            },
            React.createElement(
                "sprite",
                {
                    file = "Image/0.png", 
                    scaleX = 0.7,
                    scaleY = 0.7
                }
            )
        )
    end

    local function GameUI()
        return React.createElement(
            "draw-node",
            {
                x = -300,  
                y = 200   
            }
        )
    end

    local function GameControls(props)
        return React.createElement(
            "draw-node",
            {
                x = 400,
                y = 50
            },
            React.createElement(
                "draw-node",
                {
                    touchEnabled = true,
                    onTapped = function()
                        gameState.level = 1
                        gameState.birdsLeft = 3
                        gameState.score = 0
                        gameState.canShoot = true
                        gameState.gameOver = false
                        gameState.victory = false
                        currentBirdType = 1
                        
                        resetBirdToInitialPosition(props.bird)
                        
                    end
                },
                
                React.createElement("rect-shape", {
                    width = 100,
                    height = 50,
                    fillColor = 0xff9370D8,
                    borderWidth = 0.5,
                    borderColor = 0xffffffff
                }),
                React.createElement("label", {
                    fontName = "simsun",
                    fontSize = 20 ,
                }, "重新开始")
            ) 
        )
    end

    local function shootBird(bird, deltaX, deltaY)
        if not gameState.canShoot or gameState.birdsLeft <= 0 or not bird.current then
            return
        end
        local birdType = birdTypes[currentBirdType]
        local powerScale = 12 * birdType.power * gameState.powerMultiplier
        
        bird.current.velocity = Vec2(deltaX * powerScale, deltaY * powerScale)
        bird.current.tag = "bird"
        gameState.canShoot = false
        gameState.birdsLeft = gameState.birdsLeft - 1
    end

    local bird = useRef()
    local score = useRef()
    local world = useRef()
    local line = Line()

    local startX = 0
    local startY = 0
    local isDragging = false

    toNode(React.createElement("sprite", {
        file = "Image/10.jpg",
        x = 0,
        y = 0,
        scaleX = 2.0,
        scaleY = 2.0
    }))

    toNode(React.createElement("label", {
        fontName = "simsun", 
        fontSize = 60,
				color = Color(0xff6495ED),
        x = 0,
        y = 580
    }, "小晶晶历险记之品鉴瓯越美食"))

    toNode(React.createElement("label", {
        fontName = "simsun", 
        fontSize = 38,
        x = -800, 
        y = 320
    }, "而你，贪吃鬼晶晶的朋友，能不能\n帮助晶晶这个贪吃鬼吃到更多的\n瓯越美食呢?同时还要注意自己的\n分数能不能尽可能的高哦!试着用\n小dora撞击食物到晶晶的小嘴巴中"))

    toNode(React.createElement(
        "align-node",
        {
            windowRoot = true,
            onLayout = function(w, h)
                if world.current then
                    world.current.position = Vec2(w / 2, h / 2)
                end
            end
        },
        React.createElement(
            "physics-world",
            {
                ref = world,
                scaleX = 0.6,
                scaleY = 0.6,
                onTapBegan = function(touch)
                    local x = touch.location.x
                    local y = touch.location.y    
                    local buttonCenterX = 250 * 0.6   
                    local buttonCenterY = 200 * 0.6   
                    local buttonWidth = 80 * 0.6      
                    local buttonHeight = 30 * 0.6     
                   
                    if x >= buttonCenterX - buttonWidth/2 and x <= buttonCenterX + buttonWidth/2 and
                       y >= buttonCenterY - buttonHeight/2 and y <= buttonCenterY + buttonHeight/2 then
                        
                        gameState.birdsLeft = 3
                        gameState.score = 0
                        gameState.canShoot = true
                        gameState.gameOver = false
                        gameState.victory = false
                        currentBirdType = 1
                        
                        if score.current then
                            score.current.text = "得分 " .. gameState.score
                        end
                        
                        local function getOriginalPosition(points)
                            local positions = {
                                [10] = {x = -320, y = 30}, [15] = {x = -230, y = 30}, [20] = {x = -140, y = 30},
                                [25] = {x = -50, y = 30}, [30] = {x = 40, y = 30}, [35] = {x = 130, y = 30},
                                [40] = {x = 220, y = 30}, [45] = {x = 310, y = 30}, [50] = {x = 400, y = 30},
                                [55] = {x = -230, y = 150}, [60] = {x = -140, y = 150}, [65] = {x = -50, y = 150},
                                [70] = {x = 40, y = 150}, [75] = {x = 130, y = 150}, [80] = {x = 220, y = 150},
                                [85] = {x = 310, y = 150}, [90] = {x = -140, y = 270}, [95] = {x = -50, y = 270},
                                [100] = {x = 40, y = 270}, [105] = {x = 130, y = 270}, [110] = {x = 220, y = 270},
                                [120] = {x = -50, y = 390}, [130] = {x = 40, y = 390}, [140] = {x = 130, y = 390},
                                [200] = {x = 40, y = 510}
                            }
                            return positions[points]
                        end

                        local function getAnimationDelay(points)
                            local delays = {
                                [10] = 0, [15] = 0.1, [20] = 0.2, [25] = 0.3, [30] = 0.4,
                                [35] = 0.5, [40] = 0.6, [45] = 0.7, [50] = 0.8, [55] = 0.9,
                                [60] = 1.0, [65] = 1.1, [70] = 1.2, [75] = 1.3, [80] = 1.4,
                                [85] = 1.5, [90] = 1.6, [95] = 1.7, [100] = 1.8, [105] = 1.9,
                                [110] = 2.0, [120] = 2.1, [130] = 2.2, [140] = 2.3, [200] = 2.4
                            }
                            return delays[points] or 0
                        end
                        
                        resetBirdToInitialPosition(bird)
                        

                        local world_node = world.current
                        if world_node then
                            local children = world_node:getChildren()
                            for i = 1, #children do
                                local child = children[i]
                                if child.tag and child.tag ~= "" and child.tag ~= "bird" then
                                    local points = tonumber(child.tag)
                                    if points then
                                        child.tag = tostring(points)
                                        child.scaleX = 1
                                        child.scaleY = 1
                                        
                                        local originalPos = getOriginalPosition(points)
                                        if originalPos then
                                            child.position = Vec2(originalPos.x, originalPos.y)
                                        end
                                    end
                                end
                            end
                        end
                        
  
                        return false  
                    end
                    
                    if not gameState.canShoot then 
                        return 
                    end
                    
                    startX = touch.location.x
                    startY = touch.location.y
                    isDragging = true
                    line:clear()
                end,
                onTapMoved = function(touch)
                    if not isDragging then return end
                    
                    local currentX = touch.location.x
                    local currentY = touch.location.y
                    line:set({Vec2(startX, startY), Vec2(currentX, currentY)})
                end,
                onTapEnded = function(touch)
                    if not isDragging then return end
                    
                    isDragging = false
                    
                    local deltaX = startX - touch.location.x
                    local deltaY = startY - touch.location.y
                    local distance = math.sqrt(deltaX * deltaX + deltaY * deltaY)


                    if distance > 15 then
                        shootBird(bird, deltaX, deltaY)

                    end
                    
                    line:clear()
                end,
                onMount = function(world)
                    world:addChild(line)
                end
            },

            React.createElement(OuYueSlingshot),
            
            React.createElement(
                "body",
                {type = "Static"},
                React.createElement("rect-fixture", {
                    centerX = -200,
                    centerY = -160, 
                    width = 1370, 
                    height = 5}),
                React.createElement(
                    "draw-node",
                    nil,
                    React.createElement("rect-shape", {
                        centerX = -200,
                        centerY = -200, 
                        width = 1370, 
                        height = 5, 
                        fillColor = 0xffB0E0E6,
                        borderWidth = 0.5,
                        borderColor = 0xff000080
                    })
                )
            ),

            React.createElement(
                "draw-node",
                {x = 900, y = -220}, 
                React.createElement("sprite", {
                    file = "Image/12.png",
                    scaleX = 1, 
                    scaleY = 1
                })
            ),
            React.createElement(
                OuYueBuilding,
                {num = 10, x = -320, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 15, x = -230, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.1}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 20, x = -140, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.2}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 25, x = -50, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.3}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 30, x = 40, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.4}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 35, x = 130, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.5}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 40, x = 220, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.6}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 45, x = 310, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.7}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 50, x = 400, y = 30},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.8}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),

            React.createElement(
                OuYueBuilding,
                {num = 55, x = -230, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 0.9}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 60, x = -140, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.0}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 65, x = -50, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.1}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 70, x = 40, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.2}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 75, x = 130, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.3}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 80, x = 220, y = 150},  
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.4}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 85, x = 310, y = 150},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.5}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),

            React.createElement(
                OuYueBuilding,
                {num = 90, x = -140, y = 270},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.6}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 95, x = -50, y = 270},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.7}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 100, x = 40, y = 270},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.8}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 105, x = 130, y = 270},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 1.9}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 110, x = 220, y = 270},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 2.0}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 120, x = -50, y = 390},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 2.1}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 130, x = 40, y = 390},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 2.2}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 140, x = 130, y = 390},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 2.3}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),
            
            React.createElement(
                OuYueBuilding,
                {num = 200, x = 40, y = 510},
                React.createElement(
                    "sequence",
                    nil,
                    React.createElement("delay", {time = 2.4}),
                    React.createElement("scale", {time = 0.5, start = 0, stop = 1, easing = Ease.OutBounce})
                )
            ),

            React.createElement(
                "body",
                {
                    ref = bird,
                    type = "Dynamic",
                    x = -800, 
                    y = -50,
                    onContactStart = function(other)
                        if not other or not other.tag then 
                            return 
                        end
                        
                        local otherTag = tostring(other.tag)
                        if otherTag ~= "" and otherTag ~= "bird" then
                            local points = tonumber(otherTag)
                            if points then
                                gameState.score = gameState.score + points
                                
                                if score.current then
                                    score.current.text = "美食点数: " .. gameState.score
                                end
                                
                                if other.perform then
                                    other:perform(Scale(0.3, 0.5, 1.2))
                                end
                                      
                                if gameState.score >= gameState.targetScore then
                                    gameState.victory = true
                                end
                            end
                        end
                    end,
                    onMount = function(self)
                        self.velocity = Vec2.zero
                        self.angularVelocity = 0
                        self.angle = 0
                        self.tag = ""
                    end
                },
                React.createElement("disk-fixture", {radius = 30}),
                React.createElement(
                    "sprite",
                    {
                        file = "Image/icon.png",  
                        scaleX = 0.15,  
                        scaleY = 0.15
                    }
                )
            )
        ),

        React.createElement(GameUI),
        React.createElement(GameControls, {bird = bird}),

        React.createElement("label", {
            ref = score,
            fontName = "simsun",
            fontSize = 45,
            x = 1300,
            y = 680
        }, "本关得分: " .. gameState.score)
    ))
end
initializeGame()
return ____exports